package games;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;

import quicktime.*;
import quicktime.std.movies.Movie;
import quicktime.app.view.QTFactory;
import quicktime.io.*;

public class InstructionMovie extends Game {
	/**
	 * 
	 */
	private static final long serialVersionUID=1L;

	private static final String NAME="InstructionMovie";

	File movieFile;

	public InstructionMovie(Runner runner,String movieStr) {
		super(runner,NAME);
		
		InputStream is=InstructionMovie.class.getResourceAsStream("movies/"+movieStr);
		/*String s=url.getPath(); System.out.println(s); s=s.replaceAll("%20"," ");
		 * System.out.println(s); if(s.startsWith("/")) s=s.substring(1);
		 * System.out.println(s); /* System.out.println(url); try {
		 * System.out.println(url.toURI()); System.out.println("here");
		 * movieFile=new File(url.toURI()); System.out.println("here2"); }
		 * catch(URISyntaxException e) { System.err.println("uri
		 * exception!!!!!!!!"); e.printStackTrace(); }
		 */
		movieFile=toFile(is);
		System.out.println(movieFile.exists());
		System.out.println(movieFile.getAbsolutePath());
	}

	public void start() {}

	public void init() {

		setLayout(new BorderLayout());

		try {
			QTSession.open();
			OpenMovieFile omf=OpenMovieFile.asRead(new QTFile(movieFile));
			Movie m=Movie.fromFile(omf);
			Component c=QTFactory.makeQTComponent(m).asComponent();

			c.setPreferredSize(new Dimension(400,300));
			main=new JPanel(new BorderLayout());
			main.add(BorderLayout.NORTH,c);
			add(main);

			m.start();
		} catch(QTException e1) {
			System.err.println("Got a QuickTime exception");
			e1.printStackTrace();
		}

		go=new Painter("go","Click to Continue");
		footer=new JPanel(new BorderLayout());
		footer.add(BorderLayout.EAST,go);

		add(BorderLayout.SOUTH,footer);

		hmmm(NAME);
	}

	public static File toFile(InputStream is) {
		try {
			File f=new File("outFile.java");
			OutputStream out=new FileOutputStream(f);
			byte buf[]=new byte[1024];
			int len;
			while((len=is.read(buf))>0)
				out.write(buf,0,len);
			out.close();
			is.close();
			return f;
		} catch(IOException e) {}
		return null;
	}

	public static void main(String[] args) {
		try {
			// File f = new File("games\\GameInstructionsfiller.mov");
			JFrame mediaTest=new JFrame("Media Tester");
			mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			InstructionMovie mediaPanel=new InstructionMovie(new Runner(),"filler.mov");

			mediaPanel.init();
			mediaTest.add(mediaPanel);

			mediaTest.setSize(300,300);
			mediaTest.setVisible(true);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
